package hyg.hw;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by brss on 2018/9/19.
 */
public class Main {

    private int pbufSize;
    private class PUBF{
        public int index;
        public int offset;
        public int dataLenth;

        public PUBF(int index,int offset,int dataLenth){
            this.index = index;
            this.offset = offset;
            this.dataLenth = dataLenth;
        }
        @Override
        public String toString(){
            return this.offset+","+this.dataLenth;
        }
    }
    public void getResult(){
        Scanner sc = new Scanner(System.in);
        pbufSize = sc.nextInt();
        String s = sc.next();
        int delOffset = Integer.parseInt(s.trim().split(",")[0]);
        int delLen = Integer.parseInt(s.trim().split(",")[1]);
        ArrayList<PUBF> pubfs = new ArrayList<>();
        int temp = 0;
        while (!(s=sc.next()).equals("end")){
            int offset = Integer.parseInt(s.trim().split(",")[0]);
            int dataLenth = Integer.parseInt(s.trim().split(",")[1]);
            pubfs.add(new PUBF(temp++,offset, dataLenth));
        }
        int startPUBF = getTargetStartPUBF(delOffset);
        int startOffset = delOffset - startPUBF*pbufSize;
        int endPUBT = getEndPUBF(startPUBF,startOffset, delLen);
        //删除中间多余PUBF
        boolean isDelIn=false;//是否删除过中间
        for (int i = startPUBF + 1 ; i < endPUBT ; i++) {
            for (int j = 0 ; j < pubfs.size() ; j++) {
                if (pubfs.get(j).index == i) {
                    isDelIn = true;
                    pubfs.remove(j);
                }
            }
        }
        //处理开头的pubf
        PUBF p = getPubf(pubfs,startPUBF);
        if (isDelIn) {
            p.dataLenth = startOffset;
        }else {
            int endOffset = startOffset + delLen;
            int a = pbufSize - endOffset;
            if ( a > startOffset)//往后移动
            {
                p.dataLenth = pbufSize - delLen;
                p.offset = delLen;
            }
            else {
                //往前移动
                p.dataLenth = pbufSize - delLen;
            }
        }
        //处理结束的pubf

        p = getPubf(pubfs, endPUBT);
        if (isDelIn){
            int delEndOffset = delOffset % pbufSize;
            p.offset = delEndOffset;
            p.dataLenth -= delEndOffset;
        }

        if (pubfs.size() >= 2) {
            //判断最后一个pubf是否需要移动到前一个中去
            PUBF last = pubfs.get(pubfs.size() - 1);
            PUBF last_pre = pubfs.get(pubfs.size() - 2);


            if (last.dataLenth < (pbufSize - last_pre.dataLenth)){
                last_pre.dataLenth += last.dataLenth;
                if ( pbufSize - (last_pre.offset + last_pre.dataLenth) < last.dataLenth )
                    last_pre.offset = pbufSize - last_pre.dataLenth - last.dataLenth;
                pubfs.remove(pubfs.size() - 1);
            }
        }

        //输出结果
        for (PUBF pubf : pubfs) {
            System.out.println(pubf);
        }

    }
    private PUBF getPubf(ArrayList<PUBF> pubfs, int startPUBF) {
        for (int i = 0; i < pubfs.size() ; i++) {
            if (pubfs.get(i).index==startPUBF)
                return pubfs.get(i);
        }
        return null;
    }
    private int getEndPUBF(int startPUBF, int startOffset, int delLen) {
        int t = startOffset + delLen;
        if (t % pbufSize == 0)
            return startPUBF + t / pbufSize -1;
        else
            return startPUBF + t / pbufSize;
    }

    //获取删除开始的pbuf位置
    private int getTargetStartPUBF(int delOffset) {
        if (delOffset <= pbufSize)
            return 0;
        int i = 0;
        while (delOffset > pbufSize) {
            delOffset = delOffset - pbufSize;
            i++;
        }
        return i;
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.getResult();
    }
}
/**

2048
3000,200
0,2048
0,2048
0,1024
end

 */
